package main

import (
	"fmt"
	"reflect"
)

// 修改基本数据类型的值
func test01(a interface{})  {
	rVal := reflect.ValueOf(a)
	fmt.Println("kind is ", rVal.Kind()) // kind is  ptr

	//rVal.SetInt(200) // reflect.Value.SetInt using unaddressable value
	rVal.Elem().SetInt(200)
	// tips: rVal.Elem() rVal 必须是 ptr 或者 interface
	fmt.Println(rVal)
}

type Person struct {
	name string
	age byte
}

func test02(a interface{})  {
	rVal := reflect.ValueOf(a)
	numFiled := rVal.NumField()
	fmt.Println("file num is ", numFiled)
	for i := 0; i < numFiled; i++ {
		val := rVal.Field(i)
		fmt.Printf("field val is %v, type is %v, kind is %v\n", val, val.Type(), val.Kind())
	}
}

func main() {
	var n int = 100
	test01(& n)
	fmt.Println("after updating n is ", n)

	//fmt.Println("----------------------")
	//
	//p := Person{"jack", 12}
	//test02(p)
}
